//
//  LHRecommendSightView.m
//  Knight_Tour
//
//  Created by 庞魁 on 15-5-14.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "LHRecommendSightView.h"
#import "LHRecommendSightTableViewCell.h"
#import "LHRecommendSightModel.h"
#import "LHNetworkHandle.h"
#import "UIScrollView+MJRefresh.h"

@interface LHRecommendSightView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *sightTableView;
@property (nonatomic, retain) NSMutableArray *sightArr;
@property (nonatomic, retain) NSMutableArray *sightDetailArr;
@property (nonatomic, retain) NSMutableArray *tempArr;

@end

@implementation LHRecommendSightView

- (void)dealloc
{
    [_sightTableView release];
    [_sightArr release];
    [_sightDetailArr release];
    [_tempArr release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame cityId:(NSString *)cityId
{
    if (self = [super initWithFrame:frame]) {
        
        self.tempArr = [NSMutableArray array];
        
        [self getDataWithCityId:cityId];
    }
    
    return self;
}

/**
 *  创建推荐景点的tableView
 */
- (void)createRecommendSightTableView
{
    self.sightTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    
    _sightTableView.delegate = self;
    _sightTableView.dataSource = self;
    
    [_sightTableView addFooterWithTarget:self action:@selector(freshData)];
    [_sightTableView setFooterRefreshingText:@"客官,别心急嘛"];
    
    [_sightTableView addHeaderWithTarget:self action:@selector(tempData)];
    [_sightTableView setHeaderRefreshingText:@"我正在用力~~~"];
    
    [self addSubview:_sightTableView];
    [_sightTableView release];
    
}

#pragma mark - tableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tempArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHRecommendSightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ss"];
    
    if (cell == nil) {
        
        cell = [[[LHRecommendSightTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ss"] autorelease];
        
    }
    cell.backgroundColor =[UIColor colorWithRed:0.871 green:0.962 blue:0.965 alpha:1.000];
    cell.reSightModel = _tempArr[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.width / 3 + 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.delegate jumpToSightDetailWithIndexPath:indexPath sightArr:_tempArr];
    
}

/**
 *  获取网络数据
 */
- (void)getDataWithCityId:(NSString *)cityId
{
    NSString *a = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/%@/pois/sights/?start=0&count=40&sort=default&shift=false&latitude=38.88266356313796&longitude=121.53966915009306", cityId];
    
    
    [LHNetworkHandle getDataWithURL:a completion:^(id result) {
        
        self.sightArr = [NSMutableArray array];
        
        NSArray *arr = [result objectForKey:@"items"];
        
        for (NSDictionary *tempDic in arr) {
            
            LHRecommendSightModel *reSight = [[LHRecommendSightModel alloc] initWithDic:tempDic];
            
            [_sightArr addObject:reSight];
            
            [reSight release];
        }
        
        for (NSInteger i = 0; i < 10; i++) {
            
            [self.tempArr addObject:self.sightArr[i]];
        }
        
        
        [self createRecommendSightTableView];
    }];
    
}

- (void)freshData
{
    if (self.tempArr.count != self.sightArr.count) {
        
        NSInteger j = self.tempArr.count;
        
        for (NSInteger i = j; i < j + 5; i++) {
            
            [self.tempArr addObject:self.sightArr[i]];
        }
        
    }
    
    [_sightTableView reloadData];
    [_sightTableView footerEndRefreshing];
    
}

- (void)tempData
{
    [_sightTableView reloadData];
    [_sightTableView headerEndRefreshing];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
